﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CWY.Domain.Handlers;
using CWY.Portal.Models;

namespace CWY.Portal.Controllers
{
    public class AccountController : Controller
    {
        private AccountHandler _handler = null;

        public AccountController() { _handler = new AccountHandler(); }

        public ActionResult Index()
        {
            return View("Index", _handler.GetAccounts().Select(m => new AccountViewModel()
            {
                _AccountViewModel = new Model.Account()
                {
                    Firstname = m.Firstname,
                    Lastname = m.Lastname,
                    DisplayName = m.DisplayName,
                    DOB = m.DOB,
                    Email = m.Email,
                    Password = m.Password,
                    IsActiveUser = m.IsActiveUser
                }
            }).ToList<AccountViewModel>());
        }

        public ActionResult Add()
        {
            try
            {                
                return View("Add");
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        [HttpPost]
        public ActionResult Add(AccountViewModel _viewmodel)
        {
            try
            {
                _handler.AddAccount(_viewmodel._AccountViewModel);
                return View("Index");
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public JsonResult IsEmailAvailable(string email)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }

    }
}
